package com.dahaiwuliang.code.run;

import com.dahaiwuliang.code.server.ArcTypeService;

import com.dahaiwuliang.code.server.ArticleService;
import com.dahaiwuliang.code.server.LinkService;
import com.dahaiwuliang.code.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * 启动服务类
 * @author bzd
 */
@Component("StartupRunner")
public class StartupRunner implements CommandLineRunner {
    @Autowired
    private ServletContext application;

    @Autowired
    private ArcTypeService arcTypeService;
    @Autowired
    private LinkService linkService;
    @Autowired
    @Lazy
    private ArticleService articleService;
    @Override
    public void run(String... args) throws Exception {
     loadData();
    }

    /**
     * 加载数据到application缓存中
     */
    public  void  loadData(){


        application.setAttribute(Consts.NEW_ARTICLE,articleService.getNewArticle(10));
        application.setAttribute(Consts.CLICK_ARTICLE,articleService.getClickArticle(10));
        application.setAttribute(Consts.RANDOM_ARTICLE,articleService.getRandomArticle(10));
        application.setAttribute(Consts.ARC_TYPE_LIST,arcTypeService.listAll(Sort.Direction.ASC,"sort"));
        application.setAttribute(Consts.LINK_LIST,linkService.listAll(Sort.Direction.ASC,"sort"));



    }
}
